<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.itaoke.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  Caption <Caption@163.com> QQ 1674653
// +----------------------------------------------------------------------

namespace Wap\Controller;
use Common\Controller\FirstendController;
import("ITK.Itklib.itaoke_https");
import('ITK.ORG.Pinyin');

class SoController extends FirstendController {
	
	public function _initialize() {
		parent::_initialize();
		$this->_mod = D('Items');
		$api_config = M('ItemsSite')->where(array('code' => 'itaoke'))->getField('config');
		$this->_tbconfig = unserialize($api_config);
	}
	
	public function index(){
		
	}
	
	public function detail(){
		$num_iid=I('num_iid');
		if($num_iid==''){
			redirect(C('itk_wap_url'));
		}

		$where['num_iid']=$num_iid;
		$res=$this->_mod->where($where)->find();
		if(empty($res)){
			//如果云库存在当前用户库不存在 则根据num_iid进行api获取商品信息
			$tb_top = $this->_get_itk_top();
			$req = $tb_top->load_api('ItaokeItemDetailGetRequest');
			$req->setNumiid($num_iid);				// 设置采集数据分类ID
			$resp = (array)$tb_top->execute($req);
			if($resp['status'] == '0000')
			{
				$res = $resp['data'];
			}	
			if($res['dx']==2){
				$res['dx']=1;
			}else{
				$res['dx']=0;
			}
		}
		// 获取淘口令
		if($res['taotoken']=='' && !empty($res)){
			$apiparams=array(
				'pic'=>$res['pic_url'],
				'qurl'=>$res['quanurl'],
				'num_iid'=>$res['num_iid'],
				'title'=>$res['title'],
				'dx'=>$res['dx'],
			    'turl'=>$res['click_url'],
			);
			$res['taotoken']=$this->transform($apiparams);
		}
		
		if(!empty($res))
			$res['ulandUrl']=urlencode(buildUland($res['num_iid'],$res['quanurl'],$res['dx']));
		
		$this->_config_seo(C('itk_seo_config.item'), array(
			'title' => $res['title'],
			'intro' => $res['intro'],
			'price' => $res['price'],
			'coupon_price' => $res['coupon_price'],
			'seo_title' => $res['title'],
			'seo_keywords' => $res['title'],
			'seo_description' => $res['title'],
		));

		$this->getRandom($res['cate_id']);
		$this->assign('item',$res);
		$this->display();
	}
	
	private function _get_itk_top() {
		vendor('Itaoke.TopClient');
		vendor('Itaoke.RequestCheckUtil');
		vendor('Itaoke.Logger');
		$top = new \TopClient;
		$top->appkey = $this->_tbconfig['app_key'];
		$top->secretKey = $this->_tbconfig['app_secret'];
		return $top;
	}
	
	/**
	 * 调用爱淘客api获取佣金淘口令
	 */
	private function transform($aprams){
		$taodianjin = C('itk_taojindian_html');
		if (strpos($taodianjin, "text/javascript")) {
			$pid = get_word($taodianjin, 'pid: "' , '"');
		} else {
			$pid = $taodianjin;
		}
		//鹊桥推广
		if($aprams['dx'] != 1 && C('itk_queqiao_pid')!=''){
			$pid = C('itk_queqiao_pid');
		}
		if (!$pid) {
			return;
		}
		$aprams['pid']=$pid;
		$tb_top = $this->_get_itk_top();
		$req = $tb_top->load_api('ItaokeTaotokenRequest');
		$req->setFields($aprams);				// 设置采集数据分类ID
		$resp = (array)$tb_top->execute($req);
		if($resp['status'] != '0000')
		{
			return;
		}
		return $resp['data'];
	}
	
	public function getRandom($cate_id){

		$items = M('Items');
		$num=12;
		$where['cate_id']=$cate_id;
		$where['isq']=1;
		$where['status']='underway';
		$countcus=$items->where($where)->count();
	
		if($countcus==0){
			unset($where['cate_id']);
			$countcus=$items->where($where)->count();
		}
		$res=array();
		$min=$items->where($where)->min('id');
		$max=$items->where($where)->max('id');
		if($countcus<$num){
			$num=$countcus;
		}
		$i=1;
		$flag=0;
		while($i<=$num){
			$rundnum=rand($min, $max);
			if($flag!=$rundnum){
				if($addlink=$items->getById($rundnum) ){
					$res[]=$addlink;
					$flag=$rundnum;
					$i++;
				}
			}
		}
		$this->assign('random',$res);
	}
}